import Config from "@/config/request.ts";
import moment from "moment";
/*
获取系统信息
*/
export const systemInfo = () => {
  return uni.getSystemInfoSync();
};

/* 
获取右上角胶囊按钮位置信息
*/
export const systemMenuBtn = () => {
  // #ifdef MP-WEIXIN
  return uni.getMenuButtonBoundingClientRect();
  // #endif
  return {};
};
/*
获取路由query
*/
export const getQuery = () => {};

/**
 * 图片路径转换
 * @param {Object} img_path
 */
export const handleImg = (img_path: any) => {
  var path = "";
  if (img_path != undefined && img_path != "") {
    if (
      img_path.indexOf("http://") == -1 &&
      img_path.indexOf("https://") == -1
    ) {
      path = Config.imageUrl + "/" + img_path;
    } else {
      path = img_path;
    }
  }
  if (path.indexOf("com//") > -1) {
    path = path.replace("com//", "com/");
  }
  // console.log(path)
  return path;
};

/*
获取用户授权信息
*/
export const getSetting = async () => {
  let data = await (wx as any).getSetting({
    success: async (res: any) => {
      return res.authSetting;
    },
  });
  return data;
};
// 保存用户信息
export const saveUserinfo = () => {};

// 二进制流转字符串
export const btoa = (string: any) => {
  let b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  string = String(string);
  var bitmap,
    a,
    b,
    c,
    result = "",
    i = 0,
    rest = string.length % 3; // To determine the final padding

  for (; i < string.length; ) {
    if (
      (a = string.charCodeAt(i++)) > 255 ||
      (b = string.charCodeAt(i++)) > 255 ||
      (c = string.charCodeAt(i++)) > 255
    )
      throw new TypeError(
        "Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
      );

    bitmap = (a << 16) | (b << 8) | c;
    result +=
      b64.charAt((bitmap >> 18) & 63) +
      b64.charAt((bitmap >> 12) & 63) +
      b64.charAt((bitmap >> 6) & 63) +
      b64.charAt(bitmap & 63);
  }

  // If there's need of padding, replace the last 'A's with equal signs
  return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
};

// 获取tabbar高度
export const getTabbarHeight = () => {
  let systemInfo: any = uni.getSystemInfoSync();
  const tabbarHeight =
    systemInfo.screenHeight -
    systemInfo.windowHeight -
    systemInfo.statusBarHeight;
  return tabbarHeight;
};

export default {
  systemInfo,
  systemMenuBtn,
  getTabbarHeight,
};
